package com.skywolf.chem.manager;


import com.skywolf.chem.common.sysconfig.pojo.vo.PageVO;
import com.skywolf.chem.common.sysconfig.response.RespData;
import com.skywolf.chem.home.entity.LoopImg;
import com.skywolf.chem.home.entity.LoopImgVo;
import com.skywolf.chem.home.service.ILoopImgService;
import com.skywolf.chem.manager.param.LoopImgManagerParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 轮播图配置表 前端控制器
 * </p>
 *
 * @author 
 * @since 2020-06-09
 */
@RestController
@RequestMapping("/manager/loop-img")
public class LoopImgManagerController {

    @Autowired
    private ILoopImgService loopImgService;

    @ApiOperation(value = "获取轮播图")
    @RequestMapping(value = "/getLoopImg",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<PageVO<LoopImgVo>> getLoopImg(@RequestBody @Validated LoopImgManagerParam queryParam) {
        PageVO<LoopImgVo> obj= loopImgService.pageLoopImgList(queryParam);
        return RespData.success(obj);
    }

    @ApiOperation(value = "根据ID获取轮播图")
    @RequestMapping(value = "/getLoopImgById",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<LoopImg> getLoopImgById(@RequestParam(value = "id") Integer id) {
        LoopImg img= loopImgService.loopImgById(id);
        return RespData.success(img);
    }

    @ApiOperation(value = "创建轮播图")
    @RequestMapping(value = "/addLoopImg",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<String> addLoopImg(@RequestBody @Validated LoopImg loopImg) {
        loopImgService.save(loopImg);
        return RespData.success("success");
    }

    @ApiOperation(value = "更新轮播图")
    @RequestMapping(value = "/updateLoopImg",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<String> updateLoopImg(@RequestBody @Validated LoopImgManagerParam queryParam) {
        LoopImg img = new LoopImg();
        img.setId(queryParam.getId());
        if(StringUtils.isNotBlank(queryParam.getToUrl())){
            img.setToUrl(queryParam.getToUrl());
        }
        if(StringUtils.isNotBlank(queryParam.getImgUrl())){
            img.setImgUrl(queryParam.getImgUrl());
        }
        loopImgService.updateById(img);
        return RespData.success("success");
    }

    @ApiOperation(value = "删除轮播图",notes = "删除轮播图")
    @RequestMapping(value = "/delLoopImg",method = {RequestMethod.POST},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<String> delLoopImg(@RequestParam(value = "id") Integer id) {
        loopImgService.removeById(id);
        return RespData.success("success");
    }
}
